package vg.modules.tab_closer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventCloseTab;
import vg.services.plugin_manager.event_and_request.event.UIEventOpenNewTab;
import vg.services.plugin_manager.event_and_request.request.UIRequestCloseCurrentTab;
import vg.services.plugin_manager.event_and_request.request.UIRequestCloseTabSet;
import vg.services.user_interface_manager.interfaces.IUserInterfaceManager;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

public class SimpleTabCloser {
	// Main data
	private final Set<Integer> tabSet;
	
    /**
	 * Constructor.
	 * @param userInterfaceManager - feedback between this plugin and decoders.
	 */
	public SimpleTabCloser(final IUserInterfaceManager userInterfaceManager) {
		// initialize data
		this.tabSet = new HashSet<Integer>();
		
		// initialize components
        final JMenuItem closeTabMenuItem = new JMenuItem("Close tab");
		closeTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));
		closeTabMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_DOWN_MASK));

        final JMenuItem closeAllTabMenuItem = new JMenuItem("Close all tabs");
		//this.closeAllTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));

		// initialize listeners
		closeTabMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UIRequestCloseCurrentTab req = new UIRequestCloseCurrentTab(null);
				userInterfaceManager.sendRequest(req);
			}
		});
		
		closeAllTabMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (tabSet) {
					Set<Integer> tmpTabSet = new HashSet<Integer>(tabSet);
					UIRequestCloseTabSet req = new UIRequestCloseTabSet(null, tmpTabSet);
					userInterfaceManager.sendRequest(req);
				}
			}
		});

        // add to user interface manger
		userInterfaceManager.addMenuItem(closeTabMenuItem, "file");
		userInterfaceManager.addMenuItem(closeAllTabMenuItem, "file");
        userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch (event.getType()) {
                    case DEF_OPEN_NEW_TAB: {
                        UIEventOpenNewTab bufEvent = (UIEventOpenNewTab)event;
                        synchronized (tabSet) {
                            tabSet.add(bufEvent.getTabId());
                        }
                        break;
                    }

                    case DEF_CLOSE_TAB: {
                        UIEventCloseTab bufEvent = (UIEventCloseTab)event;
                        synchronized (tabSet) {
                            tabSet.remove(bufEvent.getTabId());
                        }
                        break;
                    }

                    default:break;
                }
            }
        });
	}
}
